package com.tanhua.server.controller;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.tanhua.server.service.TodayBestService;
import com.tanhua.server.vo.NearUserVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("tanhua")
@RestController
public class TodayBestController {

    @Autowired
    private TodayBestService todayBestService;

    /**
     * 查询今日佳人
     *
     * @return
     */
    @GetMapping("todayBest")
    public TodayBest queryTodayBest() {
        return todayBestService.queryTodayBest();
    }

    /**
     * 推荐用户列表
     *
     * @param queryParam
     * @return
     */
    @GetMapping("recommendation")
    public PageResult queryPageResult(RecommendUserQueryParam queryParam) {
        return todayBestService.queryPageResult(queryParam);
    }

    /**
     * 查询佳人信息
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}/personalInfo")
    public ResponseEntity<TodayBest> queryTodayBestInfo(@PathVariable("id") Long id){
        try {
            TodayBest todayBest = this.todayBestService.queryTodayBestInfo(id);
            if (todayBest != null){
                return ResponseEntity.ok(todayBest);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询陌生人问题
     *
     * @param userId
     * @return
     */
    @GetMapping("strangerQuestions")
    public ResponseEntity<String> queryQuestion(@RequestParam("userId") Long userId) {
        try {
            String question = this.todayBestService.queryQuestion(userId);
            return ResponseEntity.ok(question);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 回答陌生人信息
     *
     * @param param
     * @return
     */
    @PostMapping("strangerQuestions")
    public ResponseEntity<Void> replayQuestion(@RequestBody Map<String,String> param){
        Long userId = Long.valueOf(param.get("userId"));
        String reply = param.get("reply");
        try {
            boolean result = this.todayBestService.replyQuestion(userId,reply);
            if (result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 搜附近
     *
     * @param gender
     * @param distance
     * @return
     */
    @GetMapping("search")
    public ResponseEntity<List<NearUserVo>> queryNearUser(@RequestParam(value = "gender", required = false) String gender,
                                                          @RequestParam(value = "distance", defaultValue = "2000") String distance) {
        try {
            List<NearUserVo> list = this.todayBestService.queryNearUser(gender, distance);
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 展示卡片 //TODO 会推荐自己
     *
     * @return
     */
    @GetMapping("cards")
    public ResponseEntity<List<TodayBest>> queryCardsList(){
        try {
            List<TodayBest> todayBestList = this.todayBestService.queryCardsList();
            if (todayBestList != null){
                return ResponseEntity.ok(todayBestList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 右划喜欢
     *
     * @param likeUserId 喜欢的用户id
     * @return 状态码：200 | 500
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Void> saveUserLike(@PathVariable("id") Long likeUserId){
        try {
            boolean result = this.todayBestService.saveUserLike(likeUserId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 左划不喜欢
     *
     * @param likeUserId 不喜欢的用户id
     * @return 状态码：200 | 500
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Void> deleteUserLike(@PathVariable("id") Long likeUserId){
        try {
            boolean result = this.todayBestService.deleteUserLike(likeUserId);
            if(result){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
